package com.changgou.Controller;

import com.changgou.File.FastDFSFile;
import com.changgou.util.FastDFSUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

@RestController
@RequestMapping("/upload")
@CrossOrigin
public class FileUploadController {

    @PostMapping
    public Result upload(@RequestParam("file")MultipartFile file) throws Exception {
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),//获取文件名
                file.getBytes(),//获取文件字节数组
                StringUtils.getFilenameExtension(file.getOriginalFilename())
                );
        /**
         * [0] 组名
         * [1] storage文件名字
         */
        String[] uploads = FastDFSUtil.upload(fastDFSFile);

        //拼接文件地址 http://192.168.230.128:8080/group1/M00/00/00/wKjThF0DBzaAP23MAAXz2mMp9oM26.jpeg
        //http://192.168.230.128:8080/group1/M00/00/00/wKjmgGEKprGAIEhFAAXR05LADsE366.jpg" don't_cry 2
//        String url = "http://192.168.230.128:8080/"+ uploads[0] +"/" + uploads[1];
        String url = FastDFSUtil.getTrackerUrl()+ uploads[0] +"/" + uploads[1];//当nginx端口和track服务端口一致时，可以用tracker的信息代替，若不一致，则必须拼接nginx的端口

        return new Result(true, StatusCode.OK,"上传成功",url);
    }
}
